package com.accompany.service.impl;

import com.accompany.entity.Orders;
import com.accompany.dao.OrdersMapper;
import com.accompany.service.OrdersService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author DC
 * @since 2024-12-26
 */
@Slf4j
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersMapper orderMapper;

    /**
     * 分页查询订单列表
     *
     * @param page 页码
     * @param size 每页大小
     * @return 分页结果
     */
    @Override
    public Page<Orders> getPaginatedOrders(Integer page, Integer size) {
        try {
            log.info("分页查询订单列表，页码：{}, 每页大小：{}", page, size);
            Page<Orders> pageObj = new Page<>(page, size);
            Page<Orders> orderPage = baseMapper.selectPage(pageObj, null);
            orderPage.getRecords().forEach(item -> log.info(item.toString()));
            return orderPage;
        } catch (Exception e) {
            log.error("分页查询订单列表时发生错误，页码：{}, 每页大小：{}", page, size, e);
            throw e;
        }
    }

    /**
     * 获取所有订单列表
     *
     * @return 订单列表
     */
    @Override
    public List<Orders> getAllOrders() {
        try {
            log.info("获取所有订单列表");
            return orderMapper.selectList(null);
        } catch (Exception e) {
            log.error("获取所有订单列表时发生错误", e);
            throw e;
        }
    }

    /**
     * 获取单个订单信息
     *
     * @param id 订单ID
     * @return 订单对象
     */
    @Override
    public Orders getOrderById(Long id) {
        try {
            log.info("获取单个订单信息，订单ID：{}", id);
            return orderMapper.selectById(id);
        } catch (Exception e) {
            log.error("获取单个订单信息时发生错误，订单ID：{}", id, e);
            throw e;
        }
    }

    /**
     * 更新订单信息
     *
     * @param order 订单信息
     */
    @Override
    @Transactional
    public void updateOrder(Orders order) {
        try {
            log.info("更新订单信息，订单信息：{}", order.toString());
            orderMapper.updateById(order);
        } catch (Exception e) {
            log.error("更新订单信息时发生错误，订单信息：{}", order.toString(), e);
            throw e;
        }
    }

    /**
     * 删除订单
     *
     * @param id 订单ID
     */
    @Override
    @Transactional
    public void deleteOrder(Long id) {
        try {
            log.info("删除订单，订单ID：{}", id);
            orderMapper.deleteById(id);
        } catch (Exception e) {
            log.error("删除订单时发生错误，订单ID：{}", id, e);
            throw e;
        }
    }

    /**
     * 创建订单
     *
     * @param order 订单信息
     */
    @Override
    @Transactional
    public void createOrder(Orders order) {
        try {
            log.info("创建订单：{}", order.toString());
            orderMapper.insert(order);
        } catch (Exception e) {
            log.error("创建订单时发生错误，订单信息：{}", order.toString(), e);
            throw e;
        }
    }
}